package com.ucomm.talent.activity;

import java.util.ArrayList;
import java.util.Date;

import me.maxwin.view.IXListViewLoadMore;
import me.maxwin.view.IXListViewRefreshListener;
import me.maxwin.view.XListView;

import com.ucomm.talent.AppConfig;
import com.ucomm.talent.AppConstants;
import com.ucomm.talent.AppException;
import com.ucomm.talent.R;
import com.ucomm.talent.activity.detail.NewsDetailsActivity;
import com.ucomm.talent.adapter.NewsInfoListAdapter;
import com.ucomm.talent.manager.SearchManager;
import com.ucomm.talent.model.NewsInfo;
import com.ucomm.talent.widget.CustomProgressDialog;
import com.ucomm.talent.widget.MainHeaderBar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
/**
 * 行业资讯
 * @author Administrator
 *
 */
public class NewsActivity extends Activity implements IHeaderBarClickListener{
	
	private XListView listView;
	private MainHeaderBar mainHeaderBar;
	private CustomProgressDialog customProgressDialog;
	private SearchManager searchManager;
	private NewsInfoListAdapter adapter;
	private ArrayList<NewsInfo> data;
	private String url = null;
	
	Handler mHandler = new Handler(){
		public void handleMessage(Message msg){
			int what = msg.what;
			switch (what) {
			case AppConstants.MsgStatus.MSG_ERROR:
				if (customProgressDialog != null && customProgressDialog.isShowing())
                customProgressDialog.dismiss();
            ((AppException)msg.obj).makeToast(NewsActivity.this);
            	break;
	        case AppConstants.MsgStatus.MSG_SUCCUSS:
	            if (customProgressDialog != null && customProgressDialog.isShowing())
	                customProgressDialog.dismiss();
	            //放置数据
	            data.addAll((ArrayList<NewsInfo>) msg.obj);
	            if(adapter==null){
	            	adapter = new NewsInfoListAdapter(NewsActivity.this, data);
		            listView.setAdapter(adapter);
	            }else{
	            	adapter.notifyDataSetChanged();
	            }
	            
	            listView.setOnItemClickListener(new ListItemListener());
	            //上拉加载更多
                listView.setPullLoadEnable(new IXListViewLoadMore() {
                    @Override
                    public void onLoadMore() {
                    	if(AppConfig.CURR_PAGE !=0){
                    		int i = AppConfig.CURR_PAGE;
                    		if(i<AppConfig.PAGE_COUNT){
                    			AppConfig.CURR_PAGE += 1;
                    			String pageUrl = url+"&page="+AppConfig.CURR_PAGE;
                            	searchManager.searchDataFromNetwrok(pageUrl);
                    		}else{
                    			listView.disablePullLoad();
                    		}
                    	}
                    }
                });
                //下拉刷新
                listView.setPullRefreshEnable(new IXListViewRefreshListener() {
                    @Override
                    public void onRefresh() {
                    	searchManager.searchDataFromNetwrok(url);
                    	listView.stopRefresh(AppConfig.formatter.format(new Date(System.currentTimeMillis())));
                    }
                    
                });
	            break;
	        case AppConstants.MsgStatus.MSG_LOADING:
	            if (customProgressDialog != null && !customProgressDialog.isShowing())
	                customProgressDialog.show();
	            break;
	        default:
	            break;
		}
		super.handleMessage(msg);
	}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news_activity_layout);
		initView();
		searchManager = new SearchManager(this, mHandler);
		Intent i = getIntent();
	    String otherUrl = i.getStringExtra("url");
	    if (otherUrl!=null){
	    	url = otherUrl;
	    }else{
	    	url = AppConstants.NEWS;
	    }
		searchManager.searchDataFromNetwrok(url);
	}
	private void initView(){
        mainHeaderBar = (MainHeaderBar) findViewById(R.id.main_header_bar);
        mainHeaderBar.setBarClickListener(this);
        customProgressDialog = new CustomProgressDialog(this);

        listView = (XListView) findViewById(R.id.custom_list);
        listView.setmHeaderViewBackground(R.color.custom_list_item_bg_color);
        listView.setmFooterViewBackground(R.color.custom_list_item_bg_color);
        data = new ArrayList<NewsInfo>();
        
    }

	class ListItemListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			ArrayList<NewsInfo> mData = adapter.getmData();
			NewsInfo info = mData.get(arg2-1);
			Intent i = new Intent();
			i.putExtra("info", info);
			i.setClass(NewsActivity.this, NewsDetailsActivity.class);
			startActivity(i);
		}
		
	}
	@Override
	public void onBackButtOnClick(View view) {
		finish();
	}

	@Override
	public void onExitButtonOnClick(View view) {
		// TODO Auto-generated method stub
		
	}
}
